

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="深海火锅店">
  <meta name="keywords" content="">
  
    <meta name="description" content="存储过程的优点封装性通常完成一个逻辑功能需要多条SQL语句，而且各个语句之间很可能传递参数，所以，编写逻辑功能相对来说稍微复杂些，而存储过程可以把这些SQL语句包含到一个独立的单元中，使外界看不到复杂的SQL语句，只需要简单调用即可达到目的。并且数据库专业人员可以随时对存储过程进行修改，而不会影响到调用它的应用程序源代码可增强SQL语句的功能和灵活性存储过程可以用流程控制语句编写，有很强的灵活性，">
<meta property="og:type" content="article">
<meta property="og:title" content="DB19_存储过程1">
<meta property="og:url" content="http://example.com/2022/12/29/%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/%E5%A4%A7%E6%95%B0%E6%8D%AE%E5%AD%A6%E4%B9%A0/DB18&DB19/DB19_%E5%AD%98%E5%82%A8%E8%BF%87%E7%A8%8B1/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="存储过程的优点封装性通常完成一个逻辑功能需要多条SQL语句，而且各个语句之间很可能传递参数，所以，编写逻辑功能相对来说稍微复杂些，而存储过程可以把这些SQL语句包含到一个独立的单元中，使外界看不到复杂的SQL语句，只需要简单调用即可达到目的。并且数据库专业人员可以随时对存储过程进行修改，而不会影响到调用它的应用程序源代码可增强SQL语句的功能和灵活性存储过程可以用流程控制语句编写，有很强的灵活性，">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-12-29T02:00:00.000Z">
<meta property="article:modified_time" content="2022-12-29T02:10:42.000Z">
<meta property="article:author" content="深海火锅店">
<meta property="article:tag" content="大数据">
<meta name="twitter:card" content="summary_large_image">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>DB19_存储过程1 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.3","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Fluid</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/background.jpg') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="DB19_存储过程1"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-12-29 10:00" pubdate>
          2022年12月29日 上午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          4.4k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          37 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">DB19_存储过程1</h1>
            
            
              <div class="markdown-body">
                
                <h3 id="存储过程的优点"><a href="#存储过程的优点" class="headerlink" title="存储过程的优点"></a>存储过程的优点</h3><p><strong>封装性</strong><br>通常完成一个逻辑功能需要多条SQL语句，而且各个语句之间很可能传递参数，所以，编写逻辑功能相对来说稍微复杂些，而存储过程可以把这些SQL语句包含到一个独立的单元中，使外界看不到复杂的SQL语句，只需要简单调用即可达到目的。并且数据库专业人员可以随时对存储过程进行修改，而不会影响到调用它的应用程序源代码<br><strong>可增强SQL语句的功能和灵活性</strong><br>存储过程可以用流程控制语句编写，有很强的灵活性，可以完成复杂的判断和较复杂的运算。可减少网络流量<br>由于存储过程是在服务器端运行的，且执行速度快，因此当客户计算机上调用该存储过程时,<br>网络中传送的只是该调用语句，从而可降低网络负载<br><strong>高性能</strong><br>当存储过程被成功编译后，就存储在数据库服务器里了，以后客户端可以直接调用，这样所有的SQL语句将从服务器执行，从而提高性能。但需要注意的是，存储过程不是越多越好，过多的使用存储过程反而影响系统性能<br><strong>提高数据库的安全性和数据的完整性</strong><br>存储过程提高安全性的一个方案就是把它作为中间组件，存储过程里可以对某些表做相关操作，然后存储过程作为接口提供给外部程序。这样，外部程序无法直接操作数据库表，只能通过存储过程来操作对应的表，因此在一定程度上，安全性是可以得到提高的<br><strong>使数据独立</strong><br>数据的独立可以达到解耦的效果，也就是说，程序可以调用存储过程，来替代执行多条的 SQL语句。这种情况下，存储过程把数据同用户隔离开来，优点就是当数据表的结构改变时，调用表不用修改程序，只需要数据库管理者重新编写存储过程即可</p>
<h3 id="执行存储过程"><a href="#执行存储过程" class="headerlink" title="执行存储过程"></a>执行存储过程</h3><p>MySQL称存储过程的执行为调用，因此MySQL执行存储过程的语句为CALL。CALL接受存储过程的名字以及需要传递给它的任意参数</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">CALL</span> product_pricing(<span class="hljs-variable">@price</span>_low,<br>                     <span class="hljs-variable">@price</span>_hight,<br>                     <span class="hljs-variable">@price</span>_average);<br></code></pre></td></tr></table></figure>
<p>其中</p>
<ul>
<li>执行名为productpricing的存储过程，它计算并返回产品的最低、最高和平均价格。</li>
<li>存储过程可以显示结果，也可以不显示结果</li>
</ul>
<h3 id="创建一个简单地存储过程"><a href="#创建一个简单地存储过程" class="headerlink" title="创建一个简单地存储过程"></a>创建一个简单地存储过程</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">PROCEDURE</span> example.product_pricing()<br><span class="hljs-keyword">BEGIN</span><br><span class="hljs-keyword">SELECT</span> <span class="hljs-built_in">AVG</span>(prod_price) <span class="hljs-keyword">AS</span> price_average<br><span class="hljs-keyword">FROM</span> example.products;<br><span class="hljs-keyword">END</span>;<br></code></pre></td></tr></table></figure>
<p>此存储过程名为product_pricing，使用CREATEPROCEDURE example.product_pricing()进行创建;如果存储过程接收参数，所有的参数在()中列举。此存储过程有参数，但后跟的()仍然需要。BEGIN和END语句用来限定存储过程体，过程体本身仅是一个简单的SELECT语句，调用聚合AVG函数计算产品的平均价格</p>
<p>默认的MySQL语句分隔符为分号;，命令行实用程序也使用;作为语句分隔符。如果命令行实用程序要解释存储过程自身内的;字符，则它们最终不会成为存储过程的成分，这会使存储过程中的SQL出现句法错误。解决办法是临时更改命令行实用程序的语句分隔符，如下所示</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs sql">DELIMITER<span class="hljs-operator">/</span><span class="hljs-operator">/</span><br><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">PROCEDURE</span> example.product_pricing()<br><span class="hljs-keyword">BEGIN</span><br>    <span class="hljs-keyword">SELECT</span> <span class="hljs-built_in">AVG</span>(prod_price)<span class="hljs-keyword">AS</span> price_average<br>    <span class="hljs-keyword">FROM</span> example.products;<br><span class="hljs-keyword">END</span> <span class="hljs-operator">/</span><span class="hljs-operator">/</span><br>DELIMITER;<br></code></pre></td></tr></table></figure>
<p>其中，DELIMITER&#x2F;&#x2F;告诉命令行实用程序使用&#x2F;&#x2F;作为新的语句结束分隔符，可以看到标志存储过程结束的END定义为END&#x2F;&#x2F;而不是END;。这样，存储过程体内的’;’仍然保持不动，并且正确地传递给数据库引擎。最后，为恢复为原来的语句分隔符，</p>
<h3 id="删除存储过程"><a href="#删除存储过程" class="headerlink" title="删除存储过程"></a>删除存储过程</h3><p>存储过程在创建之后，被保存在服务器上以供使用，直至被删除。<br>删除存储的语法为</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">DROP</span> <span class="hljs-keyword">PROCEDURE</span> [IF <span class="hljs-keyword">EXISTS</span>] database.procedure_name<br></code></pre></td></tr></table></figure>
<p>例如删除前面创建的存储的语句为</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">DROP</span> <span class="hljs-keyword">PROCEDURE</span> example.product_pricing;<br></code></pre></td></tr></table></figure>
<p>注意不是运行存储过程，名称后面无需带()，只需要给出存储过程的名称即可<br>当存在时删除<br>如果指定的过程不存在，则DROP PROCEDURE将产生一个错误。当过程存在想删除它时(如果过程不存在也不产生错误)可使用DROP PROCEDURE IF EXISTS</p>
<h3 id="存储过程使用参数"><a href="#存储过程使用参数" class="headerlink" title="存储过程使用参数"></a>存储过程使用参数</h3><p>product pricing只是一个简单的存储过程，它简单地显示SELECT语句的结果。一般，存储过程并不显示结果，而是把结果返回给你指定的变量(参数)<br><strong>变量</strong>(variable)，内存中一个特定的位置，用来临时存储数据</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">DROP</span> <span class="hljs-keyword">PROCEDURE</span> IF <span class="hljs-keyword">EXISTS</span> example.product_pricing;<br><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">PROCEDURE</span> example.product_pricing(<br>    <span class="hljs-keyword">OUT</span> pl <span class="hljs-type">DECIMAL</span>(<span class="hljs-number">10</span>,<span class="hljs-number">2</span>),<br>    <span class="hljs-keyword">OUT</span> ph <span class="hljs-type">DECIMAL</span>(<span class="hljs-number">10</span>,<span class="hljs-number">2</span>),<br>    <span class="hljs-keyword">OUT</span> pa <span class="hljs-type">DECIMAL</span>(<span class="hljs-number">10</span>,<span class="hljs-number">2</span>)<br>)<br><span class="hljs-keyword">BEGIN</span><br>    <span class="hljs-keyword">SELECT</span> <span class="hljs-built_in">MIN</span>(prod_price),<span class="hljs-built_in">MAX</span>(prod_price), <span class="hljs-built_in">AVG</span>(prod_price)<span class="hljs-keyword">INTO</span> pl, ph, pa<br>    <span class="hljs-keyword">FROM</span> example.products;<br><span class="hljs-keyword">END</span>;<br></code></pre></td></tr></table></figure>

<ul>
<li>此存储过过程接收3个参数: pl表示最低价格，ph表示最高价格，pa表示平均价格。每个参数必须具有指定的类型</li>
<li>关键字out指出相应的参数用来从存储过程传出一个值返回给调用者.MySQL支持in、Out、Inout三种参数类型，分别表示传递给存储过程、从存储过程传出、对存储过程传入和传出存储过程</li>
<li>通过指定INTO关键字保存到相关变量</li>
</ul>
<p><strong>参数的数据类型</strong><br>存储过程的参数允许的数据类型与表中使用的数据类型相同。在定义参数时，记录集是不允许的类型，因此，不能通过一个参数返回多个行和列。<br>为调用此修改过的存储过程，必须指定3个变量名</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">CALL</span> example.product_pricing(<span class="hljs-variable">@price</span>_low, <span class="hljs-variable">@price</span>_high, <span class="hljs-variable">@price</span>_avg);<br></code></pre></td></tr></table></figure>
<p>注意:所有MySQL变量都必须以@开始。<br>在调用时，该语句不会显示任何数据，因为所有的返回值都通过变量返回</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> <span class="hljs-variable">@price</span>_low,<span class="hljs-variable">@price</span>_high, <span class="hljs-variable">@price</span>_avg;<br></code></pre></td></tr></table></figure>

<p>使用IN和OUT参数。order_total接受订单号并返回该订单的合计</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">PROCEDURE</span> order_total(<br>    <span class="hljs-keyword">IN</span> ord_num <span class="hljs-type">INT</span>,<br>    <span class="hljs-keyword">OUT</span> ord_total <span class="hljs-type">DECIMAL</span>(<span class="hljs-number">10</span>,<span class="hljs-number">2</span>)<br>)<br><span class="hljs-keyword">BEGIN</span><br>    <span class="hljs-keyword">SELECT</span> <span class="hljs-built_in">SUM</span>(item_price <span class="hljs-operator">*</span> quantity) <span class="hljs-keyword">INTO</span> ord_total<br>    <span class="hljs-keyword">FROM</span> example.orderitems<br>    <span class="hljs-keyword">WHERE</span> order_num <span class="hljs-operator">=</span> ord_num;<br><span class="hljs-keyword">END</span>;<br></code></pre></td></tr></table></figure>
<p>ord_num定义为IN，因为订单号被传入存储过程。ord_total定义为OUT，因为要从存储过程返回合计。SELECT语句使用这两个参数，WHERE子句使用ord_num选择正确的行，INTO关键字使用<br>ord_total存储计算出来的合计。</p>
<p><strong>调用</strong><br>CALL example.order_total(20005,@total);<br>必须给order_total传递两个参数;<br>第一个参数为订单号，第二个参数为包含计算出来的合计的变量名。</p>
<h3 id="复杂的存储过程"><a href="#复杂的存储过程" class="headerlink" title="复杂的存储过程"></a>复杂的存储过程</h3><p>迄今为止使用的所有存储过程基本上都是封装MySQL简单的SELECT语句。虽然它们全都是有效的存储过程例子，但它们所能完成的工作你直接用这些被封装的语句就能完成。只有在存储过程内包含业务规则和智能处理时，它们的威力才真正显现出来。<br>考虑这个场景。你需要获得与以前一样的订单合计，但需要对合计增加营业税，不过只针对某些顾客(或许是你所在省份中那些顾客)。那么，你需要做下面几件事情:</p>
<ul>
<li>获得合计</li>
<li>把营业税根据业务规则有条件地添加到合计</li>
<li>返回合计（带或者不带税)</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">DROP</span> <span class="hljs-keyword">PROCEDURE</span> IF <span class="hljs-keyword">EXISTS</span> example.order_total;<br><span class="hljs-comment">--名称: order_total</span><br><span class="hljs-comment">--参数:</span><br><span class="hljs-comment">-- ord_num =订单号码</span><br><span class="hljs-comment">-- taxable = O表示无税，1表示合税</span><br><span class="hljs-comment">-- ord_total =订单总金额,OUT类型，返回调用者</span><br><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">PROCEDURE</span> example.order_totaK(<br>    <span class="hljs-keyword">IN</span> ord_num <span class="hljs-type">INT</span>,<br>    <span class="hljs-keyword">IN</span> taxable <span class="hljs-type">BOOLEAN</span>,<br>    <span class="hljs-keyword">OUT</span> ord_total <span class="hljs-type">DECIMAL</span>(<span class="hljs-number">10</span>,<span class="hljs-number">2</span>)<br>)<br>COMMENT <span class="hljs-string">&#x27;根据给出的含税标识，计算订单总金额&#x27;</span><br><br><span class="hljs-keyword">BEGIN</span><br>    <span class="hljs-comment">--声明订单总金额变量</span><br>    <span class="hljs-keyword">DECLARE</span> total <span class="hljs-type">DECIMAL</span>(<span class="hljs-number">10</span>,<span class="hljs-number">2</span>);<br>    <span class="hljs-comment">--声明税率</span><br>    <span class="hljs-keyword">DECLARE</span> taxrate <span class="hljs-type">INT</span> DEFAULT6;<br>    <span class="hljs-comment">--计算订单总金额</span><br>    <span class="hljs-keyword">SELECT</span> <span class="hljs-built_in">SUM</span>(item_price <span class="hljs-operator">*</span> quantity) <span class="hljs-keyword">INTO</span> total<br>    <span class="hljs-keyword">FROM</span> example.orderitems<br>    <span class="hljs-keyword">WHERE</span> order_num <span class="hljs-operator">=</span> ord_num;<br>    <span class="hljs-comment">--判断是否合税，合税则需要加上税部分的金额</span><br>    IF taxable <span class="hljs-keyword">THEN</span><br>        <span class="hljs-comment">--含税，将税加到订单总金额中</span><br>        <span class="hljs-keyword">SELECT</span> total<span class="hljs-operator">*</span> (<span class="hljs-number">1</span> <span class="hljs-operator">+</span> taxrate <span class="hljs-operator">/</span> <span class="hljs-number">100</span>) <span class="hljs-keyword">INTO</span> total;<br>    <span class="hljs-keyword">END</span> IF;<br>    <span class="hljs-comment">--将结果返回参数指定的变量</span><br>    <span class="hljs-keyword">SELECT</span> total <span class="hljs-keyword">INTO</span> ord_total;<br><span class="hljs-keyword">END</span>;<br></code></pre></td></tr></table></figure>

<p>order_total存储过程有很大的变动</p>
<ul>
<li>增加了注释(前面放置–，两个减号后面一个空格开始)，在存储过程复杂性增加时，这样做特别重要</li>
<li>添加了另外一个参数taxable，它是一个布尔值(如果要增加税则为真，否则为假)</li>
<li>存储过程体中，用DECLARE语句定义了两个局部变量。DECLARE要求指定变量名和数据类型，它也支持可选的默认值(这个例子中的taxrate的默认被设置为6，注意税率为6%需要在程序进行转换)</li>
<li>SELECT语句已经改变，因此其结果存储到局部变量total而不是参数ord_total</li>
<li>IF语句检查taxable是否为真，如果为真，则用另一SELECT语句增加营业税到局部变量total</li>
<li>最后，用另一SELECT语句将total保存到参数ord_total</li>
<li>COMMENT关键字:在CREATEPROCEDURE语句中包含了一个COMMENT值。它不是必需的，但如果给出，COMMENT指定的内容将在SHOW PROCEDURE STATUS的结果中显示。</li>
</ul>
<h3 id="查看存储过程"><a href="#查看存储过程" class="headerlink" title="查看存储过程"></a>查看存储过程</h3><p>为显示用来创建一个存储过程的CREATE语句，使用SHOW CREATE PROCEDURE语句<br>SHOW CREATE PROCEDURE example.order_total\G</p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E5%A4%A7%E6%95%B0%E6%8D%AE/" class="category-chain-item">大数据</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/%E5%A4%A7%E6%95%B0%E6%8D%AE/">#大数据</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>DB19_存储过程1</div>
      <div>http://example.com/2022/12/29/数据库/MySQL/大数据学习/DB18&amp;DB19/DB19_存储过程1/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>深海火锅店</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年12月29日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/12/30/%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/%E5%A4%A7%E6%95%B0%E6%8D%AE%E5%AD%A6%E4%B9%A0/DB20&amp;DB21&amp;DB22/DB20_%E5%AD%98%E5%82%A8%E8%BF%87%E7%A8%8B-%E7%BC%96%E7%A8%8B%E5%9F%BA%E7%A1%80/" title="DB20_存储过程-编程基础">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">DB20_存储过程-编程基础</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/12/29/%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/%E5%A4%A7%E6%95%B0%E6%8D%AE%E5%AD%A6%E4%B9%A0/DB18&amp;DB19/DB18_%E5%88%86%E6%9E%90%E5%87%BD%E6%95%B02-%E7%AA%97%E5%8F%A3%E8%81%9A%E5%90%88%E5%87%BD%E6%95%B0%20copy/" title="DB18_分析函数2-窗口函数">
                        <span class="hidden-mobile">DB18_分析函数2-窗口函数</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  
<script src="//cdn.jsdelivr.net/gh/EmoryHuang/BlogBeautify@1.1/DynamicRibbon.min.js"></script>



<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
